<?php 
	require_once("header.php");
	require_once("dao/workHoursCalculation.php");
	require_once("dao/timeAttendanceDAO.php");
	
	//select all the user that the current logged in use is allow to view record for.
	//Administrator can view all users
	//Supervisor can view all users in their department
	$result = mysql_query("SELECT * FROM user WHERE id = '".$_SESSION['id']."' ORDER BY name") or die(mysql_error());
	
	$data = mysql_fetch_assoc($result);
	
	if ($data['role'] == "Administrator") {
		$result = mysql_query("SELECT * FROM user where role = 'User' ORDER BY name ASC") or die(mysql_error());
	}
	else if ($data['role'] == "Supervisor") {
		$result = mysql_query ("SELECT * FROM user where role = 'User' AND department = '".$data['department']."' ORDER BY name ASC") or die (mysql_error());
	}
	

?>


<script>
	$(function() {
		$( "#start_date" ).datepicker({
			
			showOn: "both",
			buttonImage: "../css/redmond/images/calendar.gif",
			changeMonth: true,
			buttonImageOnly: true,
			dateFormat: 'yy-mm-dd'
		});

		$( "#end_date" ).datepicker({
			showOn: "both",
			buttonImage: "../css/redmond/images/calendar.gif",
			changeMonth: true,
			buttonImageOnly: true,
			dateFormat: 'yy-mm-dd'
		});


		$(".fancybox").fancybox({
			'opacity'		    : true,
			'overlayShow'	    : true,
			'width'				: 500,
			'height'			: 700,
	        'autoScale'     	: false,
	        'transitionIn'		: 'elastic',
			'transitionOut'		: 'elastic',
			'type'				: 'iframe',
			'scrolling'         : 'no'

			//Just call $.fancybox.close() on your onClick event 
		});
	});


					
	</script>

<div id="body" class="clear">
			<div class="clear">
				<div class="column">
					 <fieldset>
        
                        <legend>View Record</legend>
                        	<form id="viewStaffForm"  method="post" action="taController.php?request=view-records&return=<?php echo $domain; ?>/viewrecords.php" enctype="multipart/form-data">
                       	    	<p><label for="staff">Staff:</label>
                            	<select name="staff" style="width: 158px">
                            		<?php if($_SESSION['role'] == "User") { ?>
                            		<option value=<?php echo $_SESSION['id']?>><?php echo $_SESSION['name']?> </option>
                            		<?php } //end if
                            		      else {
                            				while($data = mysql_fetch_assoc($result)) { ?>
                            					<option value=<?php echo $data['id']?>><?php echo $data['name']?> </option>
                            		<?php 	}
                            			  }//end else
                            		?>	
                            		
                            	</select>
                            	
                             	<p><label for="startDate">Start Date:</label>
                            	<input name="start_date" id="start_date" type="text" /></p>
                            	
                             	<p><label for="endDate">End Date:</label>
                            	<input name="end_date" id="end_date" type="text" /></p>
                            	
                     	 		<p><input name="send" class="formbutton" value="View" type="submit" /></p>
                     	 	</form>
                   	</fieldset>
                   	
                   	<div id="result"></div>
                   	<?php if(isset($_GET['result']) && $_GET['result'] == "pass") {
                   			$taRecords = json_decode($_SESSION['taRecords']);
                   			
                   			$totalNormalRateHr = 0; //variable to keep track of total min of normal rate OT
                   			$total2XRateHr = 0; //variable to keep track of total min of 2X rate OT
                   			$totalTimeInLateMins = 0;
        					$totalBreakLateMins = 0;
                   			?>
                   			
                   	
                   	<br></br>		
                   	<fieldset>
        				
                        <legend><?php echo $_GET['staff']?> (<?php echo $_GET['startDate']?> to <?php echo $_GET['endDate']?>) </legend>
                        <br></br>
                   			<table cellspacing="0">
                        		<tbody>
                        			<tr>
                            			<th>Date</th>
                            			<th>Official Time</th>
                            			<th>Start Time</th>
                            			<th>End Time</th>
                            			<th>Late</th>
                            			<th>X 1.5 Rate</th>
                            			<th>X 2 Rate</th>               
                            			<th>Break Start</th>
                            			<th>Break End</th>
                            			<th>Late</th>
                            			<th>Day</th>
                            			<th>Approval Status</th>
        							
        							</tr>
        					<?php foreach($taRecords as $record) {
        							
        							//check if record have a pending approval status, if yes, we get the data of the pending approval time and display them
        							$approvalRecord = "";
        							if($record->approval_status == 1) {
        								$approvalRecord = viewApprovalRecord($record->id, $record->date);	
        							}
        						
        							echo "<tr>";
        							$month = substr($record->date, 5, 2);
									$day = substr($record->date, 8, 2);
									$year = substr($record->date, 0, 4);
									$date = mktime(substr($record->start_time, 0, 2), substr($record->start_time, 2, 2), 0, $month, $day, $year);
									/******************************************************************************/
									/****************************** Date Column ***********************************/
        							echo "<td><a class='fancybox' href='updaterecord.php?id=".$record->id."&date=".$record->date."'>".$record->date."(".substr(date("l", $date), 0, 3).")"."</a></td>";
        							
        							/******************************************************************************/
									/****************************** Offical Time ***********************************/
        					        //if record have a pending approval status, we display the changes beside the original data
        							if($record->approval_status == 1) {
        								if($record->work_start_time != $approvalRecord['work_start_time'])
        									echo "<td>".$record->work_start_time."<font color='green'>-->".$approvalRecord['work_start_time']."</font></td>";
        								else
        									echo "<td>".$record->work_start_time. "</td>";
        							}
        							else {
        								echo "<td>".$record->work_start_time. "</td>";	
        							}
        							
        							/******************************************************************************/
									/****************************** Start Time ***********************************/
        							//if record have a pending approval status, we display the changes beside the original data
        							if($record->approval_status == 1) {
        								if($record->start_time != $approvalRecord['start_time'])
        									echo "<td>".$record->start_time."<font color='green'>-->".$approvalRecord['start_time']."</font></td>";
        								else
        									echo "<td>".$record->start_time. "</td>";
        							}
        							else {
        								echo "<td>".$record->start_time. "</td>";	
        							}
        							
        							/******************************************************************************/
									/****************************** End Time ** ***********************************/
        					        if($record->approval_status == 1) {
        								if($record->end_time != $approvalRecord['end_time'])
        									echo "<td>".$record->end_time."<font color='green'>-->".$approvalRecord['end_time']."</font></td>";
        								else
        									echo "<td>".$record->end_time. "</td>";
        							}
        							else {
        								echo "<td>".$record->end_time. "</td>";	
        							}
        							
        							/******************************************************************************/
									/****************************** Work Time Latness ***********************************/
        							
        							$startWorkLateness = calculateLateness($record->work_start_time, $record->start_time);
        					        if($startWorkLateness == ("")) {
        								echo "<td> <img src='../images/checked.gif'></img></td>";
        							}
        							else {
        								echo "<td> <img src='../images/unchecked.gif'></img>".$startWorkLateness."</td>";
        								$totalTimeInLateMins += $startWorkLateness;
        							}
        							
        							/******************************************************************************/
									/****************************** X 1.5 Rate And X 2 Rate************************/
        							
        							if($record->day == "NORMAL" || $record->day == "HALF DAY") {
        								$OTpay = calculateOTRate($record->date, $record->work_start_time, $record->end_time, $record->day);
        								$totalNormalRateHr += $OTpay;
        								echo "<td>$OTpay</td>";
        								echo "<td>0</td>";
        							}
        							else if($record->day == "OFF"){
        								$OTpay = calculate2XRate($record->work_start_time, $record->end_time, $record->break_start, $record->break_end);
        								$total2XRateHr += $OTpay;
        								echo "<td>0</td>";
        								echo "<td>$OTpay</td>";
        							}
        							
        							/******************************************************************************/
									/****************************** Break Start ***********************************/
        							//display for break_start
        					        if($record->approval_status == 1) {
        								if($record->break_start != $approvalRecord['break_start'])
        									echo "<td>".$record->break_start."<font color='green'>-->".$approvalRecord['break_start']."</font></td>";
        								else
        									echo "<td>".$record->break_start. "</td>";
        							}
        							else {
        								echo "<td>".$record->break_start. "</td>";	
        							}
        							
        							/******************************************************************************/
									/****************************** Break end ***********************************/
        							//display for break_end
        					   		if($record->approval_status == 1) {
        								if($record->break_end != $approvalRecord['break_end'])
        									echo "<td>".$record->break_end."<font color='green'>-->".$approvalRecord['break_end']."</font> </td>";
        								else
        									echo "<td>".$record->break_end. " </td>";
        							}
        							else {
        								echo "<td>".$record->break_end. " </td>";	
        							}
        							
        							/******************************************************************************/
									/****************************** Break Time Lateness ***************************/
        							$breakLateness = calculateBreakLateness($record->break_start, $record->break_end);
        							if($breakLateness == ("")) {
        								echo "<td> <img src='../images/checked.gif'></img></td>";
        							}
        							else {
        								echo "<td> <img src='../images/unchecked.gif'></img>".$breakLateness."</td>";
        								$totalBreakLateMins += $breakLateness;
        							}
        							/******************************************************************************/
									/****************************** Day ***********************************/
        					        //display for day
        					   		if($record->approval_status == 1) {
        								if($record->day != $approvalRecord['day'])
        									echo "<td>".$record->day."<font color='green'>-->".$approvalRecord['day']."</font></td>";
        								else
        									echo "<td>".$record->day. "</td>";
        							}
        							else {
        								echo "<td>".$record->day. "</td>";	
        							}
        							
        							
        							/******************************************************************************/
									/****************************** Approval Status ***********************************/
        							if($record->approval_status == "1")
        								echo "<td>Pending Approval...</td>";
        							else if($record->approval_status == "2")
        								echo "<td>Approved by ".viewApprovalDetails($record->id, $record->date)."</td>";
        							else 
        								echo "<td> </td>";
        						    echo "</tr>";
        						  }
        						  	echo "<tr>";
        						  	echo "<td> </td>";
        						  	echo "<td> </td>";
        						  	echo "<td> </td>";
        						  	echo "<td> </td>";
        						  	echo "<td>".$totalTimeInLateMins." mins</td>";
        						  	echo "<td>".$totalNormalRateHr."</td>";
        						  	echo "<td>".$total2XRateHr."</td>";
        						  	echo "<td> </td>";
        						  	echo "<td> </td>";
        						  	echo "<td>".$totalBreakLateMins." mins</td>";
        						  	echo "<td> </td>";
        						  	echo "<td> </td>";
        						  	echo "</tr>";
        					?>		
        
      							</tbody>
      						</table>
                   	<?php }?>
            	</div>
			</div>
		</div>
	


<?php 
	require_once("footer.php");
	
?>